<template>
  <div @scroll="rememPors" ref="aritcal" class="scroll-wrapper">
    <!-- 默认插槽 -->
    <!-- <van-cell :key="i" v-for="i in 20" title="单元格" value="内容">{{
            i
          }}</van-cell> -->
    <van-pull-refresh
      loading-text="手刷新"
      success-text="已经准备好"
      v-model="refreshing"
      @refresh="onRefresh"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          :to="`/article/${item.art_id.toString()}`"
          v-for="item in list"
          :key="item.art_id"
        >
          <!-- 一条新闻结构 -->
          <div
            class="article_item"
            @click="$router.push(`/article/${item.art_id}`)"
          >
            <h3 class="van-ellipsis">
              {{ item.title }}
            </h3>
            <div class="img_box" v-if="item.cover.type">
              <van-image
                :class="item.cover.type === 1 ? 'w100' : 'w33'"
                v-for="(img, i) in item.cover.images"
                :key="i"
                fit="cover"
                lazy-load
                :src="img"
              >
                <template #loading>
                  <van-loading type="spinner" size="20" />
                </template>
              </van-image>
              <!-- <van-image
                      class="w33"
                      fit="cover"
                      src="https://img.yzcdn.cn/vant/cat.jpeg"
                    />
                    <van-image
                      class="w33"
                      fit="cover"
                      src="https://img.yzcdn.cn/vant/cat.jpeg"
                    /> -->
            </div>
            <div class="info_box">
              <span>{{ item.aut_name }}</span>
              <span>{{ item.comm_count }}</span>
              <span>{{ item.pubdate | relTime }}</span>
              <span
                @click.stop="openMore(item.art_id.toString())"
                v-if="$store.state.user.token"
                @click="opactive(item.art_id)"
                class="close"
                ><van-icon name="cross"></van-icon
              ></span>
            </div>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>

    <MoreAction
      v-model="isShow"
      :artId="artid"
      @del-like="delDlike"
      @del-aaa="delaaa"
    />
  </div>
</template>
<script>
import { getListApi } from '../../../api/home'
import MoreAction from './more-action.vue'
export default {
  components: {
    MoreAction
  },
  props: {
    // 传过来的id
    channelId: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      artid: '',
      isShow: false,
      // 保存滚动的高度定义为0，从0开始
      scrollTop: 0,
      list: [],
      loading: false, // 列表加载状态
      finished: false,
      refreshing: false,
      timestamp: Date.now()
    }
  },
  activated () {
    this.articleList()
  },
  methods: {
    // 举报文章
    delaaa () {
      const index = this.list.findIndex(item => item.art_id === this.artid)
      this.list.splice(index, 1)
    },
    // 删除
    delDlike () {
      const index = this.list.findIndex(item => item.art_id === this.artid)
      this.list.splice(index, 1)
    },
    opactive (art) {
      // console.log(art)
      this.artid = art
      this.isShow = true
    },
    rememPors (e) {
      // console.log(e.target.scrollTop)
      this.scrollTop = e.target.scrollTop
    },
    articleList () {
      // 获取整个dom对象才能设置滚动高度
      this.$refs.aritcal.scrollTop = this.scrollTop
    },
    async onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      await this.$sleep(2000)
      const { data } = await getListApi({
        channel_id: this.channelId,
        timestamp: this.timestamp
      })
      // console.log(data)

      this.list.push(...data.results)
      // 加载状态结束
      this.loading = false
      if (!data.pre_timestamp) {
        this.finished = true
      } else {
        this.timestamp = data.pre_timestamp
      }
      // 关闭刷新的loadinng
      // this.refreshing = true
      this.refreshing = false
      // 数据全部加载完成
    },

    // 下拉
    onRefresh () {
      if (this.refreshing) {
        this.list = []
      }
      // 清空列表数据
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.timestamp = Date.now()
      this.onLoad()
    }
  }
}
</script>
<style lang="less" scoped>
.article_item {
  h3 {
    font-weight: normal;
    line-height: 2;
  }
  .img_box {
    display: flex;
    justify-content: space-between;
    .w33 {
      width: 33%;
      height: 90px;
    }
    .w100 {
      width: 100%;
      height: 180px;
    }
  }
  .info_box {
    color: #999;
    line-height: 2;
    position: relative;
    font-size: 12px;
    span {
      padding-right: 10px;
      &.close {
        border: 1px solid #ddd;
        border-radius: 2px;
        line-height: 15px;
        height: 12px;
        width: 16px;
        text-align: center;
        padding-right: 0;
        font-size: 8px;
        position: absolute;
        right: 0;
        top: 7px;
      }
    }
  }
}
</style>
